import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path';
import svgLoader from "./src/plugins/svgicon/loader";

const pathResolve = (dir: string): any => {
  return resolve(__dirname, '.', dir);
};

const alias: Record<string, string> = {
  '@': pathResolve('./src'),
};

// https://vitejs.dev/config/
export default defineConfig(({mode}) => {
  const envConfig = loadEnv(mode, './');
  // 本地联调配置对象
  const proxyLocalPath = envConfig.VITE_API_BASE_API;

  const proxys = {}
  if (proxyLocalPath) {
    proxys[proxyLocalPath] = {
      target: envConfig.VITE_API_URL,
      changeOrigin: true,
      rewrite: (path) => {
        const path2 = path.replace(/^\/dev-api/, '')
        return path2.replace(/^\/cpmp-service/, '')
      }
    }
  }
  return {
    plugins: [
      vue(),
      svgLoader('./src/assets/svg/'),
    ],
    root: process.cwd(),
    resolve: {alias},
    base: mode === 'development' ? '/' : './',
    server: {
      host: '0.0.0.0',
      port: 9090,
      open: false,
      proxy: {
        ...proxys,
        [envConfig.VITE_API_BASE_API]: {
          target: envConfig.VITE_API_LDP_URL,
          changeOrigin: true,
          rewrite: (path) => {
            return path.replace(/^\/dev-api/, '')
          }
        },
      }
    },
    build: {
      outDir: '宝宝定制'
    },
    // assetsInclude: '**/assets/**',
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              },
            },
          },
        ],
      },
    },
    /*optimizeDeps: {
      include: [
        'monaco-editor'
      ]
    }*/
  }
})
